<?php

namespace App\Module\Record;

use App\Module\BaseModule;
use App\Service\DictService;
use App\depository\RecordTalkDep;
use App\depository\SteamIDEosidDep;

class TalkModule extends BaseModule
{
    public function init($request)
    {
        $dictService = new DictService();
        $dictService->setTeams();

        $data = [
            'dict' => $dictService->getDict(),
        ];

        return self::response($data);
    }


    public function add($request)
    {
        $param = $request->input();

        $teamId = $request->headers->get('TeamId');

        $dep = new RecordTalkDep();
        $mapDep = new SteamIDEosidDep();

        if($param['steamid']){
            $resMap = $mapDep->get_fields('eosid','steamid',$param['steamid']);
        }
        $data = [
            'date' => $param['date'],
            'gh_id'=> $teamId,
            'steamid' => $param['steamid'],
            'player_name' => $param['player_name'],
            'team_name' => $param['team_name'],
            'team_id' => $param['team_id'],
            'camp' => $param['camp'],
            'eosid' => $resMap ? $resMap->eosid : '',
        ];

        $dep->add($data);

        return self::response();
    }

    public function del($request)
    {
        $param = $request->input();

        $dep = new RecordTalkDep();

        $dep->del($param['id']);
        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();

        $teamId = $request->headers->get('TeamId');
        $dep = new RecordTalkDep();
        $mapDep = new SteamIDEosidDep();

        if($param['steamid']){
            $resMap = $mapDep->get_fields('eosid','steamid',$param['steamid']);
        }
        $data = [
            'date' => $param['date'],
            'gh_id'=> $teamId,
            'steamid' => $param['steamid'],
            'player_name' => $param['player_name'],
            'team_name' => $param['team_name'],
            'team_id' => $param['team_id'],
            'camp' => $param['camp'],
            'eosid' => $resMap ? $resMap->eosid : '',
        ];

        $dep->upd($param['id'],$data);

        return self::response();

    }

    public function list($request)
    {
        $param = $request->input();

        $teamId = $request->headers->get('TeamId');

        $param['gh_id'] = $teamId;
        $param['per_page'] = $param['per_page'] ?? 50;
        $param['current_page'] = $param['current_page'] ?? 1;

        $dep = new RecordTalkDep();
        $resList = $dep->list($param);

        $data['list'] = $resList->map(function($item)  {
            return [
                'id' => $item->id,
                'gh_id' => $item->gh_id,
                'date' => $item->date,

                'steamid' => $item->steamid,
                'player_name' => $item->player_name,
                'channel' => $item->channel,
                'content' => $item->content,
            ];
        });

        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];

        return self::response($data);
    }

}

